package com.comando.util;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.comando.dto.UserDto;

public class CookieUtil {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(CookieUtil.class);

	//public static final String USER_COOKIE = "_qunawan";

	public static Cookie addCookie(HttpServletResponse response, String cookieName, 
			String cookieValue, String domain , int time) {
		Cookie cookie = new Cookie(cookieName, cookieValue);
		//if(time!=-1)
			cookie.setMaxAge(time); 
		cookie.setDomain(domain); 
		cookie.setPath("/");
		response.addCookie(cookie);
		return cookie;
	}

	// 得到cookie
	public static String getCookie(HttpServletRequest request, String cookieName) {
		Cookie[] cookies = request.getCookies(); 
		logger.debug("cookies: " + cookies);
		if (cookies != null) {
			for (Cookie cookie : cookies) {
				logger.debug("cookie: " + cookie);
				if (cookieName.equals(cookie.getName())) {
					String value = cookie.getValue();
					return value;
				}
			}
		}
		return "";
	}

}
